
$(function(){
    var mobile=document.getElementById('mobile')//手机号
    var code=document.getElementById('code')//验证码
    var setCode = document.getElementById("setCode");//确认验证
    var pwd=document.getElementById('pwd')//密码
    var pwdtwo=document.getElementById('pwdtwo')//确认密码
    var pho=mobile.value
    var phone_sign=/^1[3-8]\d{9}$/i;
    $('header i').click(function(){
        window.history.back()
    })
    //返回登录
    $('.login').click(function(){
        window.location.href='http://www.lvyou.com/index/login/login'
    })
    //定时器
    function alerts(info){
        $(".alert").css("display","block").find("p").text(info);
        timer=setTimeout(function(){
            $(".alert").css("display","none");
            clearTimeout(timer);
        },2000)
    }
    //注册
    $('#reg').click(function(){
        if(mobile.value==''){
            alerts("请输入手机号！")
            return false;
        }
        if(!phone_sign.test(mobile.value)){
            alerts("手机号格式不正确！")
            return false;
        }
        if(code.value==''){
            alerts("请输入验证码！")
            return false;
        }
        if($('#code').val()!=$('#setCode').val()){
            alerts("验证码不正确！")
            return false;
        }
        if(pwd.value==''){
            alerts("请输入密码！")
            return false;
        }
        if(pwdtwo.value==''){
            alerts("请输入确认密码！")
            return false;
        }
        if(pwdtwo.value!=pwd.value){
            alerts("两次输入的密码不一致！")
            return false;
        }else{
            $.ajax({
                type:"post",
                url:"http://www.lvyou.com/index/Register/register",
                async:true,
                data:{
                    mobile:mobile.value,
                    password:pwdtwo.value,
                },
                success:function(data){
                    if(data.code==0){
                        alerts("注册失败，请重新再试")
                    }else if(data.code==1){
                        $('.zhezhao').css('display','block')
                        $('#tip').css('display','block')

                    }
                }
            });
        }
    })

    //获取验证码
    $('#obtain').click(function(){
        if(mobile.value==''){
            alerts("请输入手机号！")
            return false;
        }else if(!phone_sign.test(mobile.value)){
            alerts("手机号格式不正确！")
            return false;
        }else{
            $.ajax({
                type:"post",
                url:"http://www.lvyou.com/index/Phone/vailatePhone",
                async:true,
                data:{
                    mobile:mobile.value
                },
                success:function(data){
                    if(data==1){
                        alerts("该手机号已存在,请重新输入！")
                        return false;
                    }
                    else if(data==0){
                        $.ajax({
                            type:"post",
                            url:"http://www.lvyou.com/index/Register/sendRegisterCode",
                            async:true,
                            data:{
                                mobile:mobile.value
                            },
                            success:function(data){
                                console.log(data)
                                if(data.code==0){
                                    alerts('验证码发送失败')
                                }else{
                                    document.getElementById('setCode').value= data.data.register_code;
                                    //显示遮罩层
                                    $('.zhezhao1').css('display','block')
                                    alerts('验证码发送成功')
                                    var i=60
                                    var timer=setInterval(function(){
                                        i=i-1
                                        $('#obtain').text(i)
                                        if(i==0){
                                            clearInterval(timer)
                                            $('#obtain').text('重新发送')
                                            //关闭遮罩层
                                            $('.zhezhao1').css('display','none')
                                        }
                                    },1000)
                                }
                            }
                        });
                    }
                    //}
                }
            });
        }
    })
})
